package com.cfjjc.gz.usraccount.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cfjjc.gz.common.action.PagedAction;
import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.util.StringUtil;
import com.cfjjc.gz.common.util.TreeConfig;
import com.cfjjc.gz.usraccount.service.A_USR_GRPService;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
import com.cfjjc.gz.usraccount.vo.A_USR_GRPVO;

/**
 * 
 * 说明：对 用户组信息表 进行增删改查的前端处理类
 * 
 * @author  liuxun
 * @since 2012-03-06
 * @version 1.0
 */
public class A_USR_GRPAction extends PagedAction  {

    /**
	 * 
	 */
	private static final long serialVersionUID = 5183198131639760383L;

	private A_USR_GRPVO model = new A_USR_GRPVO();

	private TreeConfig tree = new TreeConfig();
	
    @Autowired
    private A_USR_GRPService a_USR_GRPService;

    public A_USR_GRPAction() {
        super();
		this.setQUERY_NAME("browseA_USR_GRPList");
    }

    // 显示用户组信息表管理首页
    public String showIndex() throws Exception {
        return INDEX;
    }

    // 显示用户组信息表列表页面
    public String queryList() throws Exception {
        preparePagedList(QUERY_NAME, getModel());
        if (!pagedRecords.exists()) {
            try {
                a_USR_GRPService.queryA_USR_GRPs((A_USR_GRPVO) pagedRecords.getParam(), pagedRecords);
            } catch (OTSException e) {
                this.addActionError(e.getMessage());
				return ERROR;
            }
        }
        return LIST;
    }

    // 显示树
	public String showTree() throws Exception {
		if(model.getUsr_grp_status() == null || "".equals(model.getUsr_grp_status())){
			tree.setUrl("A_USR_GRP!queryTreeNode.do");
		}else{
			tree.setUrl("A_USR_GRP!queryTreeNode.do?model.containsDeleted=" + model.getContainsDeleted()+"&model.usr_grp_status=" + model.getUsr_grp_status());
		}
		
		return TREE;
	}

	// 显示用户组管理中的树
	public String queryTreeNode() throws Exception {
		Map root = null;

		// 显示一级节点时，自动添加根节点
		if (id == null || id.length == 0) {
			root = new HashMap();
			root.put("id", "0");
			root.put("text", "用户组树");
		} else {
			
		}

		List<A_USR_GRPVO> nodes = null;

		try {
			nodes = a_USR_GRPService.queryA_USR_GRPList(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}

		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
		for (A_USR_GRPVO node : nodes) {
			Map<String, Object> item = new HashMap<String, Object>();

			item.put("id", node.getUsr_grp_code());
			item.put("text", node.getUsr_grp_code() + " " + node.getUsr_grp_name());
			item.put("attributes", node);
			item.put("state", "open");
			items.add(item);
		}

		if (root != null) {
			root.put("children", items);
			this.rows.add(root);
		} else {
			this.rows = items;
		}

		return TREE_NODE;
	}
	
    // 显示用户组信息表的明细页面
	public String showInfo() throws Exception {
		try {
			if (StringUtil.isEmpty(model.getKey())) {
				return INFO;
			}

			A_USR_GRPVO param=new A_USR_GRPVO();
        	param.setKey(model.getKey());
			model=a_USR_GRPService.queryA_USR_GRP(param);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		return INFO;
	}

    // 显示用户组信息表录入页面
    public String showAdd() throws Exception {
        setOperationType(SystemConst.OPERATION_TYPE_NEW);
        return INPUT;
    }

    // 保存用户组信息表录入
    public String doInsert() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setCreate_by(usr.getUsr_id());
        	model.setNode_id(SystemConst.NODE_ID);
            a_USR_GRPService.doInsertA_USR_GRP(model);
        } catch (OTSException e) {
            this.addActionError(e.getMessage()); 
            return ERROR;
        }
        return OPE_INFO;
    }

    // 显示修改页面
    public String showEdit() throws Exception {
       try {
       		if (StringUtil.isEmpty(model.getKey())) {
				return INPUT;
			}

        	A_USR_GRPVO param=new A_USR_GRPVO();
        	param.setKey(model.getKey());
            model=a_USR_GRPService.queryA_USR_GRP(param);
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
			return ERROR_PAGE;
        }

        setOperationType(SystemConst.OPERATION_TYPE_MODIFY);
        return INPUT;
    }

    // 保存对用户组信息表的修改
    public String doUpdate() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
            a_USR_GRPService.doUpdateA_USR_GRP(model);
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
            return ERROR;
        }
        return OPE_INFO;
    }

    // 删除用户组信息表。允许批量删除。允许部分删除成功
    public String doDelete() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
            for (int n = 0; n < id.length; n++) {
                A_USR_GRPVO param = new A_USR_GRPVO();
                param.setKey(id[n]);
                param.setUpdate_by(usr.getUsr_id());
                a_USR_GRPService.doDeleteA_USR_GRP(param);
            }
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
            return ERROR;
        }
        return NONE;
    }    

    /** 以下为set,get方法 */
    public Object getModel() {
        return model;
    }
    
	public TreeConfig getTree() {
		return tree;
	}
}
